package com.xc.pay.ali.ali_native;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.xc.pay.ali.modal.AliNativePayModal;
import com.xc.pay.common.PayUrl;

/**
 * Filename: AliNativePay.java <br>
 * <p>
 * Description: 支付宝扫码支付<br>
 *
 * @author: rongrong <br>
 * @version: 1.0 <br>
 * @Createtime: 2019-12-08 <br>
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ---------------------------------------------------------*
 * 2019-12-08       rongrong            v1.0              第一次创建
 * @Copyright: Copyright (c)2019 by rongrong <br>
 */
public class AliNativePay {


    public String native_pay(AliNativePayModal payModal){

        AlipayClient client = new DefaultAlipayClient(PayUrl.SERVER_URL, payModal.getAppid(),
                payModal.getApp_private_key(), PayUrl.FORMAT, PayUrl.CHARSET, payModal.getApp_publice_key(), PayUrl.SIGN_TYPE);
        AlipayTradePrecreateRequest alipay_request = new AlipayTradePrecreateRequest();
        // 封装请求支付信息
        AlipayTradePrecreateModel model=new AlipayTradePrecreateModel();
        model.setOutTradeNo(payModal.getOutTradeNo());
        model.setTotalAmount(payModal.getTotal_amount());
        model.setSubject(payModal.getSubject());
        model.setBody(payModal.getBody());
        model.setTimeoutExpress("30m");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(payModal.getNotify_url());
        String qrCode = "";
        try {
            AlipayTradePrecreateResponse execute = client.execute(alipay_request);
            if(execute.isSuccess() && execute.getCode().equals("10000")){
                qrCode = execute.getQrCode();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return qrCode;
    }


}
